<?php
/**
 * Created by PhpStorm.
 * User: hd-1
 * Date: 2018/8/23
 * Time: 21:33
 */
namespace app\admin\controller;

use app\common\lib\Tool;
use think\Db;
use think\Request;

class Cooperative extends Base
{
    public function index(){
        $Arr = input();
        if(!empty($Arr['cooperativeName'])){
            $map[] =  ['cooperativeName', 'like', "%".$Arr['cooperativeName']."%"];
        }else{
            $map = [];
        }
        $data = Db::table('cooperative')
            ->where($map)
            ->order('id asc')
            ->paginate(5,false,['query' => request()->param()]);
        $page = $data->render();
        return view('cooperative/cooperative',['data'=>$data,'page'=>$page,'map'=>$Arr]);
    }

    public function add(Request $request){
        if($request->isPost()){
            $Arr = input();
            $data = [
                'cooperativeName'=>$Arr['cooperativeName'],
                'imageUrl'=>$Arr['imageUrl'],
                'add_time'=>time(),
            ];
            $Ret = Db::name('cooperative')->insert($data);
            if($Ret){
                return Tool::json(1,[],'添加合作伙伴成功');
            }else{
                return Tool::json(0,[],'添加合作伙伴失败');
            }
        }else{
            return view('cooperative/add');
        }
    }

    public function update(Request $request){
        $Arr = input();
        if($request->isPost()){
            $data = [
                'cooperativeName'=>$Arr['cooperativeName'],
                'imageUrl'=>$Arr['imageUrl'],
                'add_time'=>time(),
            ];
            $map['id'] = $Arr['id'];
            $Ret = Db::table('cooperative')->where($map)->update($data);
            if($Ret){
                return Tool::json(1,[],'修改合作伙伴成功');
            }else{
                return Tool::json(0,[],'修改合作伙伴失败');
            }
        }else{
            $data = Db::table('cooperative')
                ->where(['id'=>$Arr['id']])
                ->find();
            return view('cooperative/update',['data'=>$data]);
        }
    }

    public function deleteCooperative(){
        $id = input('id');
        $Ret = Db::table('cooperative')->where('id',$id)->delete();
        if($Ret){
            return Tool::json(1,[],'删除合作伙伴成功');
        }else{
            return Tool::json(0,[],'删除合作伙伴失败');
        }
    }

    public function AboutUpdate(Request $request){
        $Arr = input();
        if($request->isPost()){
            $data = [
                'content'=>$Arr['myEditor'],
            ];
            $map['id'] = $Arr['id'];
            $Ret = Db::table('about')->where($map)->update($data);
            if($Ret){
                return Tool::json(1,[],'修改成功');
            }else{
                return Tool::json(0,[],'修改失败');
            }
        }else{
            $data = Db::table('about')
                ->where(['id'=>$Arr['id']])
                ->find();
            return view('about/order_update',['data'=>$data]);
        }
    }
}